#pragma once
#ifndef CLIB_TCP_BUFFER_H
#define CLIB_TCP_BUFFER_H

#include "clib_type.h"
#include "clib_queue.h"
#ifdef __cplusplus
extern "C" {
#endif


//#include "clib_ip.h"
//#include "clib_listen.h"

/** TODO 整理 queue 梳理 **/

struct tcp_frag_node 
{
    clib_queue_t frag_list;
    u32_t        seq;               /**< TCP SEQ **/
    u16_t        offset;            /**< 数据偏移 **/
    u16_t        length;            /**< 数据长度 **/
};

typedef struct tcp_buffer_s tcp_buffer_t;

struct tcp_buffer_s
{
    clib_queue_t    used;           /**< 使用的节点 **/
    clib_queue_t    free;           /**< 未使用的节点 **/
    u16_t           size;           /**< 总长度 **/
    u16_t           node;           /**< node数量 **/
    u16_t           ready_len;      /**< 准备好的长度 **/
    u16_t           frag_len;       /**< 碎片总长度 **/
    u32_t           seq;            /**< TCP SEQ **/
    u32_t           ack;            /**< TCP ACK **/
    u16_t           node_offset;    /**< node 读取偏移 **/
    u16_t           init;           /**< 填充 **/
    u16_t           offset;         /**< value 偏移记录 **/
    u16_t           value_len;      /**< value 总长度 **/
    u08_t          *value;          /**< value 指针 **/
    u08_t           data[];
};

i32_t 
clib_tcp_buffer_init(tcp_buffer_t *buffer, u16_t size, u16_t node_n);

i32_t 
clib_tcp_buffer_set_seq(tcp_buffer_t *buffer, u32_t seq);

i32_t 
clib_tcp_buffer_recv_put_data(tcp_buffer_t *buffer, u32_t seq, u08_t* data, u16_t data_len);

i32_t 
clib_tcp_buffer_recv_get_data(tcp_buffer_t *buffer, u08_t* data, u16_t data_len);

i32_t 
clib_tcp_buffer_send_put_data(tcp_buffer_t *buffer,  u08_t* data, u16_t data_len);

i32_t 
clib_tcp_buffer_send_get_data(tcp_buffer_t *buffer, u32_t seq, u08_t* data, u16_t *data_len);

i32_t 
clib_tcp_buffer_send_ack_data(tcp_buffer_t *buffer, u32_t ack);

#ifdef __cplusplus
}
#endif

#endif 